﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Serialization;
using Common;
using FreeSql;
using Model;
using Server;

namespace BearingOiling
{
    /// <summary>
    /// CfgView.xaml 的交互逻辑
    /// </summary>
    public partial class CfgView : Window
    {
        private CfgView()
        {
            InitializeComponent();
            this.Closed += CfgView_Closed;
            this.Loaded += CfgView_Loaded;
        }

        private void CfgView_Loaded(object sender, RoutedEventArgs e)
        {
            DB = Servers.DB;

            FreshData();
        }

        private void CfgView_Closed(object sender, EventArgs e)
        {
            _view = null;
        }

        private static CfgView _view;
        public static CfgView GetView()
        {
            if (_view == null)
            {
                _view = new CfgView();
            }

            return _view;
        }

        private List<Map> Maps { get; set; }

        private void DataGrid_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (DataGrid.SelectedItem == null)
            {
                return;
            }

            if (DataGrid.SelectedItem is Map map)
            {
                TextBoxNo.Text = map.Id.ToString();
                TextBoxTagVal.Text = map.TagVal.ToString();
                TextBoxBearingNo.Text = map.BearingNo;
            }
        }
        private void FreshData()
        {
            Maps = new List<Map>();
            Maps = DB.Select<Map>().Where(p => p.Id > 0).ToList();
            DataGrid.ItemsSource = Maps;
        }

        private IFreeSql DB;
        /// <summary>
        /// 新建数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddBtn_OnClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TextBoxBearingNo.Text) || string.IsNullOrEmpty(TextBoxTagVal.Text))
            {
                MessageBox.Show("填写数据不正确，请确认");
                return;
            }

            if (int.TryParse(TextBoxTagVal.Text, out int tagVal))
            {
                if (Maps.Count(p => p.TagVal == tagVal) > 0)
                {
                    MessageBox.Show("当前数值已经存在匹配关系，请检查");
                    return;
                }

                try
                {
                    var count = DB.Insert(new Map() { BearingNo = TextBoxBearingNo.Text, TagVal = tagVal, TimeStamp = DateTime.Now }).ExecuteAffrows();
                    if (count > 0)
                    {
                        ResetTxtBox();
                        FreshData();
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show("新建Map数据失败");
                    Logger.WriteSysLog("新建Map数据失败", exception);
                }
            }
            else
            {
                MessageBox.Show("输入数据不合法");
            }
        }


        private void EditBtn_OnClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TextBoxNo.Text))
            {
                MessageBox.Show("请先选中数据");
                return;
            }
            if (string.IsNullOrEmpty(TextBoxBearingNo.Text) || string.IsNullOrEmpty(TextBoxTagVal.Text))
            {
                MessageBox.Show("填写数据不正确，请确认");
                return;
            }

            if (int.TryParse(TextBoxNo.Text, out int num) && int.TryParse(TextBoxTagVal.Text, out int tagVal))
            {
                try
                {
                    var count = DB.Update<Map>().Set(p => p.BearingNo, TextBoxBearingNo.Text).Set(p => p.TagVal, tagVal)
                         .Set(p => p.TimeStamp, DateTime.Now).Where(p => p.Id == int.Parse(TextBoxNo.Text))
                         .ExecuteAffrows();
                    if (count < 1)
                    {
                        MessageBox.Show("更新失败");
                    }
                    else
                    {
                        FreshData();
                        ResetTxtBox();
                    }
                }
                catch (Exception exception)
                {
                    Logger.WriteSysLog("更新Map数据失败", exception);
                }
            }
            else
            {
                MessageBox.Show("输入数据不合法");
            }
        }

        private void DelBtn_OnClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TextBoxNo.Text))
            {
                MessageBox.Show("请先选中数据");
                return;
            }

            int id = int.Parse(TextBoxNo.Text);
            try
            {
                var count = DB.Delete<Map>().Where(p => p.Id == id).ExecuteAffrows();
                if (count > 0)
                {
                    ResetTxtBox();
                    FreshData();
                }
            }
            catch (Exception exception)
            {
                Logger.WriteSysLog("删除数据失败", exception);
            }
        }
        /// <summary>
        /// 清楚textbox的数据
        /// </summary>
        private void ResetTxtBox()
        {
            TextBoxNo.Text = "";
            TextBoxBearingNo.Text = "";
            TextBoxTagVal.Text = "";
        }
    }
}
